import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Map;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class OptionView extends JPanel implements ActionListener
{
	private ArrayList<JComponent> dropDowns;
	private Controller control;
	private JFrame parent;
	private JPanel end;
	
	public OptionView(Controller c, JFrame pman)
	{
		end=new JPanel();
		control = c;
		//setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setLayout(new FlowLayout());
		dropDowns=new ArrayList<JComponent>();
		parent=pman;
		add(getFirstDropDown());
		//add(new JButton("A"));
		//add(new JButton("A"));
		//add(new JButton("A"));
		//add(new JButton("A"));
		validate();
	}
	public JComboBox getFirstDropDown() 
	{
		String[] beginData=control.getNextMenu("Algorithms and Data Structures", 0);
		JComboBox beginDrop = new JComboBox(beginData);
		//beginDrop.setMaximumSize(new Dimension(20,20));
		//beginDrop.setPreferredSize(beginDrop.getMaximumSize());
		dropDowns.add(beginDrop);
		beginDrop.addActionListener(this);
		
		parent.pack();
		parent.repaint();
		return beginDrop;

	}
	
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		JComboBox selected=(JComboBox) e.getSource();
		int index=dropDowns.indexOf(selected);

		String[] newDropArray = control.getNextMenu((String)selected.getSelectedItem(), index);
		if (newDropArray!=null)
		{
			//for (int i=index+1;i<dropDowns.size();i++)
			for (int i=dropDowns.size()-1;i>=index+1;i--)
			{
				remove(dropDowns.get(i));
				dropDowns.remove(i);
			}
			remove(end);
			JComboBox newDrop=new JComboBox(newDropArray);
			newDrop.addActionListener(this);
			dropDowns.add(newDrop);
			add(newDrop);
		}
		else
		{
			remove(end);
			end=new JPanel();
			JButton go=new JButton("Go!");
			parent.pack();
		/*	JPanel itemPanel=new JPanel();
			itemPanel.add(new Label("Initial # of Items in Data Structure: "));
			JTextField numItems=new JTextField();
			numItems.setMinimumSize(new Dimension(60,20));
			numItems.setPreferredSize(new Dimension(60,20));
			itemPanel.add(numItems);
			add(itemPanel);*/
			
			JPanel maxPanel=new JPanel();
			maxPanel.add(new Label("Maximum Size: "));
			final JTextField maxSize=new JTextField();
			maxSize.setMinimumSize(new Dimension(60,20));
			maxSize.setPreferredSize(new Dimension(60,20));
			maxPanel.add(maxSize);
			
			
			/*JPanel intervalPanel=new JPanel();
			intervalPanel.add(new Label("Interval: "));
			final JTextField interval=new JTextField();
			interval.setMinimumSize(new Dimension(60,20));
			interval.setPreferredSize(new Dimension(60,20));
			intervalPanel.add(interval);*/
			
			
			
			go.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					try
					{
						//control.dealWithInfo(Integer.parseInt(numItems.getText()));
						int max=Integer.parseInt(maxSize.getText());
						//int val=Integer.parseInt(interval.getText());
						int val=1;
						if (max <=1 || val<=0)
						{
							JOptionPane.showMessageDialog(new JFrame(), "Make sure your maximum n is above 1!");
							return;
						}
						control.dealWithInfo(max, val);
						parent.repaint();
						parent.repaint();
					//}catch(Exception e)
						parent.pack();
					}catch(Exception e)
					{
						JOptionPane.showMessageDialog(new JFrame(), "Make sure your maximum is an actual number!!");
					}
				}
				
			});
			//dropDowns.add(itemPanel);
			
			/*dropDowns.add(maxPanel);
			dropDowns.add(intervalPanel);
			dropDowns.add(go);
			add(maxPanel);
			add(intervalPanel);
			add(go);*/
			
			
			end.add(maxPanel);
			//end.add(intervalPanel);
			end.add(go);
			add(end);
			//dropDowns.add(end);
			//remove(dropDowns.get(dropDowns.size()-1));
			
		}
		parent.pack();
	}
	

}
